export const getTextImg = ({
    arrayText,
    multiple = 2,
    color = '#000000',
    backgroundColor = '#ffffff',
    size = 13,
    shadow = false,
    shadowColor = '#ffffff',
}) => {
    const canvas = document.createElement('canvas')

    const context = canvas.getContext('2d')
    canvas.height = arrayText.length * ((size + 2) * multiple)
    context.font = `normal ${size}px PingFang SC,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji`
    let textWidth = 0
    arrayText.forEach(item => {
        const textW = context.measureText(item).width
        if (textWidth < textW) {
            textWidth = textW
        }
    })
    canvas.width = (textWidth + 1) * multiple

    // 绘制一个半透明的矩形
    context.fillStyle = backgroundColor
    context.fillRect(0, 0, (textWidth + 1) * multiple, (size + 2) * multiple * arrayText.length)

    context.font = `normal ${
        size * multiple
    }px PingFang SC,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji`
    context.fillStyle = color

    const drawTextWithShadow = ({ offsetX, offsetY, item, index }) => {
        if (shadow) {
            // 清除之前的阴影设置
            context.shadowOffsetX = 0
            context.shadowOffsetY = 0
            context.shadowBlur = 0
            context.shadowColor = 'transparent'

            // 添加新的阴影效果
            context.shadowOffsetX = offsetX
            context.shadowOffsetY = offsetY
            context.shadowBlur = 0
            context.shadowColor = shadowColor
        }

        context.fillText(item, 0, (index + 1) * (size * multiple))
    }
    arrayText.forEach((item, index) => {
        // 定义阴影效果
        drawTextWithShadow({ offsetX: 1.5, offsetY: 1.5, index, item })
        drawTextWithShadow({ offsetX: -1.5, offsetY: 1.5, index, item })
        drawTextWithShadow({ offsetX: 1.5, offsetY: -1.5, index, item })
        drawTextWithShadow({ offsetX: -1.5, offsetY: -1.5, index, item })
    })

    return {
        width: textWidth + 1,
        src: canvas,
        // .toDataURL('image/png')
    }
}
